package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import service.UserService;
import util.MD5Util;
import util.RSAUtil;

@Controller
public class RegisterController {
	private UserService userservice = new UserService();

	@RequestMapping("/register")
	public ResponseEntity<JSONObject> register(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		String password = request.getParameter("password");
		String username = request.getParameter("username");
		System.out.println("用户名:" + username);
		System.out.println("加密密码：" + password);
		
		JSONObject json = new JSONObject();
		String privateKey = (String) request.getSession().getAttribute("privateKey");
		String descrypedPwd = new String(RSAUtil.decryptByPrivateKey(password, privateKey));
		System.out.println("解密后的密码"+descrypedPwd);
		String md5password = MD5Util.MD5(descrypedPwd);
		System.out.println("md5加密后的密码"+md5password);
		boolean result = userservice.register(username,md5password);
		json.put("result", result);
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json,HttpStatus.OK);
		return responseResult;
	}
}